import { request } from '@/utils/request'

/**
 * 获取课程列表
 * @param {Object} params 查询参数
 * @param {number} params.page 页码
 * @param {number} params.size 每页数量
 * @param {string} params.keyword 关键词
 * @param {string} params.category 分类
 * @param {string} params.level 难度等级
 * @param {string} params.status 状态
 * @param {string} params.sort 排序方式
 */
export function getCourseList(params) {
  return request.get('/courses', params)
}

/**
 * 获取课程详情
 * @param {string} courseId 课程ID
 */
export function getCourseDetail(courseId) {
  return request.get(`/courses/${courseId}`)
}

/**
 * 获取我的课程列表
 * @param {Object} params 查询参数
 * @param {number} params.page 页码
 * @param {number} params.size 每页数量
 * @param {string} params.status 学习状态：learning/completed/expired
 */
export function getMyCourses(params) {
  return request.get('/courses/my', params)
}

/**
 * 报名课程
 * @param {string} courseId 课程ID
 */
export function enrollCourse(courseId) {
  return request.post(`/courses/${courseId}/enroll`)
}

/**
 * 取消报名
 * @param {string} courseId 课程ID
 */
export function unenrollCourse(courseId) {
  return request.delete(`/courses/${courseId}/enroll`)
}

/**
 * 获取课程章节列表
 * @param {string} courseId 课程ID
 */
export function getCourseChapters(courseId) {
  return request.get(`/courses/${courseId}/chapters`)
}

/**
 * 获取章节详情
 * @param {string} courseId 课程ID
 * @param {string} chapterId 章节ID
 */
export function getChapterDetail(courseId, chapterId) {
  return request.get(`/courses/${courseId}/chapters/${chapterId}`)
}

/**
 * 获取课程视频列表
 * @param {string} courseId 课程ID
 * @param {string} chapterId 章节ID
 */
export function getChapterVideos(courseId, chapterId) {
  return request.get(`/courses/${courseId}/chapters/${chapterId}/videos`)
}

/**
 * 获取视频详情和播放地址
 * @param {string} courseId 课程ID
 * @param {string} videoId 视频ID
 */
export function getVideoDetail(courseId, videoId) {
  return request.get(`/courses/${courseId}/videos/${videoId}`)
}

/**
 * 记录视频学习进度
 * @param {string} courseId 课程ID
 * @param {string} videoId 视频ID
 * @param {Object} data 进度数据
 * @param {number} data.currentTime 当前播放时间
 * @param {number} data.duration 视频总时长
 * @param {boolean} data.completed 是否完成
 */
export function updateVideoProgress(courseId, videoId, data) {
  return request.put(`/courses/${courseId}/videos/${videoId}/progress`, data)
}

/**
 * 获取课程学习进度
 * @param {string} courseId 课程ID
 */
export function getCourseProgress(courseId) {
  return request.get(`/courses/${courseId}/progress`)
}

/**
 * 获取课程资料列表
 * @param {string} courseId 课程ID
 * @param {string} chapterId 章节ID（可选）
 */
export function getCourseMaterials(courseId, chapterId) {
  const url = chapterId 
    ? `/courses/${courseId}/chapters/${chapterId}/materials`
    : `/courses/${courseId}/materials`
  return request.get(url)
}

/**
 * 下载课程资料
 * @param {string} courseId 课程ID
 * @param {string} materialId 资料ID
 */
export function downloadMaterial(courseId, materialId) {
  return request.download(`/courses/${courseId}/materials/${materialId}/download`)
}

/**
 * 获取课程作业列表
 * @param {string} courseId 课程ID
 * @param {Object} params 查询参数
 * @param {string} params.status 状态：pending/submitted/graded
 */
export function getCourseAssignments(courseId, params) {
  return request.get(`/courses/${courseId}/assignments`, params)
}

/**
 * 获取作业详情
 * @param {string} courseId 课程ID
 * @param {string} assignmentId 作业ID
 */
export function getAssignmentDetail(courseId, assignmentId) {
  return request.get(`/courses/${courseId}/assignments/${assignmentId}`)
}

/**
 * 提交作业
 * @param {string} courseId 课程ID
 * @param {string} assignmentId 作业ID
 * @param {Object} data 作业数据
 * @param {string} data.content 作业内容
 * @param {Array} data.attachments 附件列表
 */
export function submitAssignment(courseId, assignmentId, data) {
  return request.post(`/courses/${courseId}/assignments/${assignmentId}/submit`, data)
}

/**
 * 上传作业附件
 * @param {string} courseId 课程ID
 * @param {string} assignmentId 作业ID
 * @param {File} file 文件
 */
export function uploadAssignmentFile(courseId, assignmentId, file) {
  return request.upload(`/courses/${courseId}/assignments/${assignmentId}/upload`, file)
}

/**
 * 获取课程讨论列表
 * @param {string} courseId 课程ID
 * @param {Object} params 查询参数
 * @param {number} params.page 页码
 * @param {number} params.size 每页数量
 * @param {string} params.type 类型：question/discussion
 */
export function getCourseDiscussions(courseId, params) {
  return request.get(`/courses/${courseId}/discussions`, params)
}

/**
 * 发布讨论
 * @param {string} courseId 课程ID
 * @param {Object} data 讨论数据
 * @param {string} data.title 标题
 * @param {string} data.content 内容
 * @param {string} data.type 类型
 */
export function createDiscussion(courseId, data) {
  return request.post(`/courses/${courseId}/discussions`, data)
}

/**
 * 回复讨论
 * @param {string} courseId 课程ID
 * @param {string} discussionId 讨论ID
 * @param {Object} data 回复数据
 * @param {string} data.content 回复内容
 */
export function replyDiscussion(courseId, discussionId, data) {
  return request.post(`/courses/${courseId}/discussions/${discussionId}/replies`, data)
}

/**
 * 获取课程公告列表
 * @param {string} courseId 课程ID
 * @param {Object} params 查询参数
 */
export function getCourseAnnouncements(courseId, params) {
  return request.get(`/courses/${courseId}/announcements`, params)
}

/**
 * 获取课程评价列表
 * @param {string} courseId 课程ID
 * @param {Object} params 查询参数
 */
export function getCourseReviews(courseId, params) {
  return request.get(`/courses/${courseId}/reviews`, params)
}

/**
 * 提交课程评价
 * @param {string} courseId 课程ID
 * @param {Object} data 评价数据
 * @param {number} data.rating 评分
 * @param {string} data.comment 评价内容
 */
export function submitCourseReview(courseId, data) {
  return request.post(`/courses/${courseId}/reviews`, data)
}

/**
 * 收藏课程
 * @param {string} courseId 课程ID
 */
export function favoriteCourse(courseId) {
  return request.post(`/courses/${courseId}/favorite`)
}

/**
 * 取消收藏课程
 * @param {string} courseId 课程ID
 */
export function unfavoriteCourse(courseId) {
  return request.delete(`/courses/${courseId}/favorite`)
}

/**
 * 获取收藏的课程列表
 * @param {Object} params 查询参数
 */
export function getFavoriteCourses(params) {
  return request.get('/courses/favorites', params)
}

/**
 * 获取课程分类列表
 */
export function getCourseCategories() {
  return request.get('/courses/categories')
}

/**
 * 搜索课程
 * @param {Object} params 搜索参数
 * @param {string} params.keyword 关键词
 * @param {string} params.category 分类
 * @param {string} params.level 难度
 * @param {number} params.page 页码
 * @param {number} params.size 每页数量
 */
export function searchCourses(params) {
  return request.get('/courses/search', params)
}

/**
 * 获取推荐课程
 * @param {Object} params 参数
 * @param {number} params.limit 数量限制
 */
export function getRecommendedCourses(params) {
  return request.get('/courses/recommended', params)
}

/**
 * 获取热门课程
 * @param {Object} params 参数
 * @param {number} params.limit 数量限制
 */
export function getPopularCourses(params) {
  return request.get('/courses/popular', params)
}

/**
 * 获取最新课程
 * @param {Object} params 参数
 * @param {number} params.limit 数量限制
 */
export function getLatestCourses(params) {
  return request.get('/courses/latest', params)
}